package com.ray.juc;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.TimeUnit;

public class ExceptionTest {

    /**
     * 并发访问异常，多线程一边读一边写
     * ConcurrentModificationException
     *
     *
     */

    public static void main(String[] args) {
        
//       final List<String> list = new ArrayList<String>();
//        final List<String> list = new Vector<String>();
        final List<String> list = Collections.synchronizedList(new ArrayList<String>());
//        final List<String> list = new CopyOnWriteArrayList<String>();
//        final Set<String> list = new CopyOnWriteArraySet<String>();
//        final Map<String,String> list = new ConcurrentHashMap<String, String>();
        for (int i = 0; i < 10; i++) {
            new Thread(new Runnable() {
                public void run() {
                    try {
                        TimeUnit.MILLISECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    list.add("a");
                    System.out.println(list);
                }
            }).start();
        }
    }

}
